package com.voicechat.firstmet.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.MeInfoService;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.PopUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity_imessage
 * @ClassName: ServiceAndHelp
 * @Description: 客服与帮助页面
 * @Author: ljp
 * @CreateDate: 2018/10/15 17:05
 * @Version: 1.0
 */
public class ServiceAndHelpActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_contact)
    TextView tvContact;
    @BindView(R.id.et_back)
    EditText etBack;
    @BindView(R.id.tv_sum)
    TextView tvSum;
    @BindView(R.id.et_way)
    EditText etWay;
    @BindView(R.id.tv_sure_submit)
    TextView tvSureSubmit;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String deviceInfo;

    private String TAG = "ServiceAndHelp";
    private boolean isContent;
    private boolean isWay;

    @Override
    protected int setStatusBarColor() {
        return 0;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }
    @Override
    protected int loadLayout() {
        return R.layout.activity_service_and_help;
    }
    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        deviceInfo = PhoneUtils.getDeviceInfo(context);
        initView();
    }
    private void initView() {
        tvSureSubmit.setEnabled(false);
        etBack.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String trim = s.toString().trim();
                if(!TextUtils.isEmpty(trim)){
                    isContent = true;
                }else {
                    isContent = false;
                }
                if(isContent && isWay){
                    tvSureSubmit.setEnabled(true);
                    tvSureSubmit.setBackgroundResource(R.drawable.login_btn);
                }else {
                    tvSureSubmit.setEnabled(false);
                    tvSureSubmit.setBackgroundResource(R.drawable.login_btn_2);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {
                tvSum.setText(s.length()+"/300");
            }
        });
        etWay.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String trim = s.toString().trim();
                if(!TextUtils.isEmpty(trim)){
                    isWay = true;
                }else {
                    isWay = false;
                }
                if(isContent && isWay){
                    tvSureSubmit.setEnabled(true);
                    tvSureSubmit.setBackgroundResource(R.drawable.login_btn);
                }else {
                    tvSureSubmit.setEnabled(false);
                    tvSureSubmit.setBackgroundResource(R.drawable.login_btn_2);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    @OnClick({R.id.linear_back, R.id.tv_contact, R.id.tv_sure_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            case R.id.tv_contact:
                showPopu(view);
                break;
            case R.id.tv_sure_submit:
                if (!Utils.isMobileNO(etWay.getText().toString().trim())) {
                    toast_MakeText("手机号码格式不正确，请重新输入");
                }else {
                    submitHttp();
                }
                break;
        }
    }
    /**
    * @method
    * @description 联系客服popu
    * @date: 2018/11/6 18:03
    * @author: ljp
    * @return
    */
    private void showPopu(View v) {
        View view = View.inflate(context,R.layout.popu_service_help,null);
        PopUtils.showPopupWindow(activity,view,false);
        LinearLayout imageView = view.findViewById(R.id.image_close);
        TextView tvSure = view.findViewById(R.id.tv_sure);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(PopUtils.isPopShowing()){
                    PopUtils.hidePopupWindow();
                }
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneNumber = getResources().getString(R.string.service_phone);
                Intent dialIntent =  new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));//跳转到拨号界面，同时传递电话号码
                startActivity(dialIntent);
                PopUtils.hidePopupWindow();
            }
        });

    }
    /**
    * @method
    * @description 客服与帮助提交
    * @date: 2018/11/6 16:51
    * @author: ljp
    * @return
    */
    private void submitHttp() {
        String content = etBack.getText().toString().trim();
        String way = etWay.getText().toString().trim();
        MeInfoService.serviceAndHelp(mySeleInfo.getId(),SPUtils.getEditoString(SPUtils.SP_TOKEN),deviceInfo,content,way).subscribe(result -> {
            Log.i(TAG, "submitHttp: ----------"+result);
            if(result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS){
                toast_MakeText(result.getMsg());
                Intent intent = new Intent(activity,AllSuccessActivity.class);
                intent.putExtra("flag","5");
                startActivity(intent);
                finish();
            }else if(result.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(result.getMsg());
            }
        },throwable -> {

        });


    }
}
